<?php
namespace frontend\controllers;

use common\models\Wxuser;
use frontend\models\User;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\filters\VerbFilter;

/**
 * Class WxwebController
 * @package frontend\controllers
 */
class WxwebController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * 创建菜单
     * Displays homepage.
     *  $this->createMenu([
     *      [
     *           'type' => 'view',
     *           'name' => '绑定用户',
     *           'key' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect '
     *      ],
     *      [
     *           'type' => 'view',
     *           'name' => '小普商城',
     *           'url' => 'http://www.soso.com'
     *      ]
     *      ...
     * ]);
     * @return mixed
     */
    public function actionCreatemenu()
    {
        /** @var \callmez\wechat\sdk\Wechat $wechat $wechat */
        $wechat = Yii::$app->wechat;
        $url = 'http://120.76.213.82/wx/frontend/web/index.php?r=wxweb/bind';
        $bandingUrl = $wechat->getOauth2AuthorizeUrl($url,'authorize','snsapi_userinfo');
        $param = [
            [
                'type' => 'view',
                'name' => '绑定用户',
                'url' => $bandingUrl
            ]
        ];
        $wechat->createMenu($param);
        return 0;
    }

    /**
     * 获取微信用户信息
     */
    public function actionBind()
    {
        $code = isset($_GET['code'])?$_GET['code']:'';
        if($code) {
            /** @var \callmez\wechat\sdk\Wechat $wechat $wechat */
            $wechat = Yii::$app->wechat;
            try{
                $wxInfo = $wechat->getOauth2AccessToken($code);
                $openid = $wxInfo['openid'];
                $menberInfo = $wechat->getMemberInfo($openid);

                $user = Wxuser::findOne(['openid'=>$openid]);
                if($user){
                    return '绑定成功';
                }
                $user = new Wxuser();
                $user->openid = $openid;
                $user->unionid = $wxInfo['unionid'];
                $user->wx_name = $menberInfo['nickname'];
                $user->wx_images = $menberInfo['headimgurl'];
                $user->wx_name = $menberInfo['nickname'];
                $user->username = $menberInfo['nickname'];
                $user->sex = $menberInfo['sex'];
                if($user->save()){
                    Yii::$app->session->set('openid',$openid);
                    Yii::$app->session->set('unionid',$openid);
                    Yii::$app->session->set('wx_name',$menberInfo['nickname']);
                    Yii::$app->session->set('wx_images', $menberInfo['headimgurl']);
                    Yii::$app->session->set('wx_name',$menberInfo['nickname']);
                    print_r($user);exit;
                }
                print_r($user->errors);exit;
            }catch (Exception $e){
                return $e->getMessage();
            }
        }
    }

}
